from common.base_z import Base, By, get_driver


class ProductcategoryPage(Base):  # 页面类(对象)继承Base基础类
    """封装表现层:制作每个元素的定位器"""
    # 登录页面元素定位
    close_X = (By.XPATH, '//div[@class="panel-cross"]/span')
    button_loc = (By.ID, 'cloudLogin')
    username_loc = (By.NAME, 'username')
    password_loc = (By.NAME, 'password')
    login_loc = (By.CLASS_NAME, 'btn-a')
    # 商品分类页面元素定位
    merchandise_management_loc = (By.XPATH, '//ul[@id="menu-ul"]/li[2]')  # 定位商品管理元素
    main_frame_loc = (By.ID, 'main-frame')
    commodity_classification_loc = (By.ID, 'sub-menu-03_category_list')  # 定位商品分类
    goods_transfer_loc = (By.CSS_SELECTOR, r"#\32 _27 > td:nth-child(8) > a:nth-child(1)")  # 大家电转移商品按钮定位器
    target_cat_loc = (By.CSS_SELECTOR,
                      'body > div.main-div > form > table > tbody > tr:nth-child(2) > td > select:nth-child(4)')  # 转移到'下拉框定位器
    delete_goods_classify = (By.CSS_SELECTOR, r'#\33 _32> td:nth-child(8) > a:nth-child(3)')  # 冰箱移除按钮定位器
    goods_classify_loc = (By.XPATH, '//div[@id="panelCloud"]//following-sibling::h1/a')  # 定位添加分类按钮
    category_name_loc = (By.NAME, 'cat_name')  # 定位分类名称输入框
    category_select_loc = (By.NAME, 'parent_id')  # 定位上级分类下拉框
    number_unit_loc = (By.NAME, 'measure_unit')  # 定位数量单位输入框
    sort_loc = (By.NAME, 'sort_order')  # 定位排序输入框
    is_show_loc = (By.XPATH, '//tr[@id="measure_unit"]/following-sibling::tr[2]/td[2]/input')  # 定位是否显示的 '是' 选择框
    not_show_loc = (By.XPATH, '//tr[@id="measure_unit"]/following-sibling::tr[2]/td[2]/input[2]')  # 定位是否显示的 '否' 选择框
    navigation_is_show_loc = (
        By.XPATH, '//tr[@id="measure_unit"]/following-sibling::tr[3]/td[2]/input')  # 定位是否显示在导航栏的 '是' 选择框
    navigation_not_show_loc = (
        By.XPATH, '//tr[@id="measure_unit"]/following-sibling::tr[3]/td[2]/input[2]')  # 定位是否显示在导航栏的 '否' 选择框
    confirm_submit_loc = (By.XPATH, '//table[@id="general-table"]/following-sibling::div/input')  # 定位确认提交按钮
    reset_submit_loc = (By.XPATH, '//table[@id="general-table"]/following-sibling::div/input[2]')  # 定位重置按钮

   # 封装操作层

    def click_close_x(self):  # 点击关闭激活系统
        self.click(self.close_X)

    def click_button(self):  # 点击其他登录方式按钮
        self.click(self.button_loc)

    def input_username(self, username):  # 输入用户名  username:用户名
        self.send_keys(self.username_loc, username)

    def input_password(self, password):  # 输入密码  password:密码
        self.send_keys(self.password_loc, password)

    def click_login(self):  # 点击登录按钮
        self.click(self.login_loc)

    def merchandise_iframe(self):  # 进入左边导航栏的iframe
        self.iframe(1)

    def merchandise_management(self):  # 点击商品管理
        self.click(self.merchandise_management_loc)

    def commodity_classification(self):  # 点击商品分类
        self.click(self.commodity_classification_loc)

    def quit_iframe(self):  # 退出当前iframe
        self.default_iframe()

    def content_iframe(self):  # 进入新的iframe
        self.iframe_element(self.main_frame_loc)

    def goods_classify(self): # 点击商品分类按钮
        self.click(self.goods_classify_loc)

    def category_name(self, category):  # 输入分类名称
        self.send_keys(self.category_name_loc, category)

    def category_select(self):  # 上级分类下拉框选择
        self.select_index(self.category_select_loc, 0)

    def number_unit(self, number):  # 输入数量单位  number：数量单位
        self.send_keys(self.number_unit_loc, number)

    def clear_sort(self):  # 清空排序输入框内容
        self.clear(self.sort_loc)

    def sort_number(self, sort):  # 输入排序内容  sort：排序内容
        self.send_keys(self.sort_loc, sort)

    def confirm_submit(self):  # 点击确认按钮
        self.click(self.confirm_submit_loc)


if __name__ == '__main__':
    product = ProductcategoryPage(get_driver())
    product.get('http://localhost:8080/ecshop/admin/privilege.php?act=login')
    product.click_close_x()
    product.click_button()
    product.input_username('root1234')
    product.input_password('root1234')
    product.click_login()
    product.merchandise_iframe()
    product.merchandise_management()  # 点击商品管理
    product.commodity_classification()  # 点击商品分类
    product.quit_iframe()
    product.content_iframe()
    product.goods_classify()
    product.category_name('高达W')
    product.category_select()
    product.number_unit('60')
    product.clear_sort()
    product.sort_number('30')
    product.confirm_submit()
    product.quit()
